package com.budwk.app.sqfwV2.generator.dayingRoadExcel;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfwDoc.bo.SqfwExcelForSummaryBO;
import com.budwk.app.sqfwDoc.bo.SqfwExcelJudgeRes;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.SqfwExcelCreateCommonBO;
import com.budwk.app.sqfwV2.dto.dayingRoad.DaYingLuRedRecordPdfDTO;
import com.budwk.app.sqfwV2.dto.dayingRoad.DaYingLuWhiteRecordPdfDTO;
import com.budwk.app.sqfwV2.dto.image.DaYingLuWhiteRecordExcelImageConfigBO;
import com.budwk.app.sqfwV2.enums.RedWhite;
import com.budwk.app.util.ClassUtil;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;

import java.util.List;
import java.util.Map;

/**
 * excel表九：区（市、县）信息互通统计表
 */
public class WhiteInformationStatisticsExcelGenerator extends BaseTemplateExcelGenerator {

    @Override
    protected String getTemplateName() {
        return "daYingLu/white/informationStatistics.xlsx";
    }

    /**
     * 可以参照 {@link com.budwk.app.sqfw.enums.DownLoadFileNameEnum}
     *
     * @return 文件名名称
     */
    @Override
    public String getDownloadFileName() {
        //TODO 在这里返回你生成的文件名名称
        // 名称要求带上随机数。如果仅仅是一个中文名称的话，上传到对象存储会造成文件覆盖
        // 注意注意注意不用带上后缀
        return "表九：区（市、县）信息互通统计表";
    }

    /**
     * 用户自定义excel基础参数，比如表头这样的公用数据
     *
     * @param excelParam       已经填充的基础数据据
     * @param excelCreateParam excel生成提供的原料
     */
    @Override
    protected void customerPopularSheetBaseParam(Map<String, Object> excelParam, SqfwExcelCreateCommonBO excelCreateParam) {
        // 是否只生成模板
        Boolean onlyGenerateTemplateFlag = excelCreateParam.getExcelCreateConfig().getOnlyGenerateTemplateFlag();
        if (onlyGenerateTemplateFlag) {
            for (String key : excelParam.keySet()) {
                excelParam.put(key, "");
            }
            return;
        }

        excelParam.put("phone", excelCreateParam.getBaseExcelParamDTO().getCustomerParamMap().get("phone"));
    }

    @Override
    protected void customerPopularSheetDataParam(List<Map<String, Object>> listMap, SqfwExcelCreateCommonBO excelCreateParam) {
        // 是否只生成模板
        Boolean onlyGenerateTemplateFlag = excelCreateParam.getExcelCreateConfig().getOnlyGenerateTemplateFlag();
        if (onlyGenerateTemplateFlag) {
            List<Map> mapList = JSONUtil.toList(JSONUtil.toJsonStr(listMap), Map.class);
            listMap.clear();
            for (Map<String, Object> map : mapList) {
                for (String key : map.keySet()) {
                    map.put(key, "");
                }
                listMap.add(map);
                return;
            }
        }

        int num = 0;
        for (Map map : listMap) {
            num ++;
            map.put("num", num);
        }
    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, SqfwExcelForSummaryBO forSummaryBO) {
        JSONObject jsonObject = JSON.parseObject(forSummaryBO.getSqfwFormSummaryNew().getParamData());
        rowDataMap.put("community", "街道、村（居委会）");
        if (RedWhite.RED.getName().equals(jsonObject.get(ClassUtil.getField(DaYingLuRedRecordPdfDTO::getCelebratoryEvents)))) {
            rowDataMap.put("type", RedWhite.RED.getAnotherName());
            String description = jsonObject.get(ClassUtil.getField(DaYingLuRedRecordPdfDTO::getNameMarriagePerson)).toString() +
                    rowDataMap.get(ClassUtil.getField(DaYingLuRedRecordPdfDTO::getMarriageCertificateProcessingTime)) + "结婚";
            rowDataMap.put("description", description);
            String dispose = "经"+forSummaryBO.getSqfwFormSummaryNew().getCommunity()+
                    "红白理事摸排，酒席地址："+jsonObject.get(ClassUtil.getField(DaYingLuRedRecordPdfDTO::getBanquetLocation))+"，" +
                    "红白理事会成员已上门进行宣传移风易俗取消低俗婚闹。家属已承诺安全有序进行婚礼。";
            rowDataMap.put("dispose", dispose);
        } else {
            rowDataMap.put("type", RedWhite.WHITE.getAnotherName());
            String description = jsonObject.get(ClassUtil.getField(DaYingLuWhiteRecordPdfDTO::getDeceasedName)).toString() +
                    rowDataMap.get(ClassUtil.getField(DaYingLuWhiteRecordPdfDTO::getDeceasedDeathTime)) + "死亡";
            rowDataMap.put("description", description);
            String dispose = "经"+forSummaryBO.getSqfwFormSummaryNew().getCommunity() +
                    "红白理事摸排，治丧地点："+rowDataMap.get(ClassUtil.getField(DaYingLuWhiteRecordPdfDTO::getFuneralLocation)) +
                    "，治丧"+rowDataMap.get(ClassUtil.getField(DaYingLuWhiteRecordPdfDTO::getFuneralDays)) +
                    "天，红白理事会成员已上门进行宣传移风易俗丧事简办厚养薄葬，自觉简化治丧仪式。家属承诺简办丧事。";
            rowDataMap.put("dispose", dispose);
        }
    }

    /**
     * 如果在遍历之前就能够判断excel是否生成，那么就在这个方法中进行判断
     *
     * @param excelCreateParam excel生成所需的最为全面的数据
     * @param excelParam       excel已经填充的部分数据
     */
    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeErgodic(SqfwExcelCreateCommonBO excelCreateParam, Map<String, Object> excelParam) {
        SqfwExcelJudgeRes sqfwExcelJudgeRes = new SqfwExcelJudgeRes();
        sqfwExcelJudgeRes.setFileIdentity(excelCreateParam.getExcelCreateConfig().getFileIdentity());
        if (excelCreateParam.getDataList() != null && excelCreateParam.getDataList().size() > 0) {
            sqfwExcelJudgeRes.setCreateConditionFlag(true);
        } else {
            sqfwExcelJudgeRes.setCreateConditionFlag(false);
        }
        return sqfwExcelJudgeRes;
    }

    /**
     * 如果在遍历之后能够判断excel是否生成，那么就在这个方法中进行判断
     *
     * @param excelParam excel已经填充的部分数据
     */
    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeCreate(Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return DaYingLuWhiteRecordExcelImageConfigBO.class;
    }
}
